import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

// 最新的
Widget ffSliverAppBar2({
  required BuildContext context,
  required double height,
  required Widget child,
  bool floating = false,
  bool pinned = false, // true 推不走
  bool snap = false,
}) {
  return MediaQuery.removePadding(
    context: context,
    removeBottom: true,
    removeTop: true,
    child: SliverAppBar(
      elevation: 0.0,
      leading: Container(),
      floating: floating,
      pinned: pinned,
      snap: snap,
      flexibleSpace: child,
      collapsedHeight: height,
      expandedHeight: height,
      toolbarHeight: 0,
      primary: true,
    ),
  );
}

// 这是一个详情的举例
Widget ffsliverAppBarTest() {
  return PreferredSize(
    preferredSize: Size.fromHeight(kToolbarHeight),
    child: SliverAppBar(
      pinned: true,
      elevation: 0.0,
      backgroundColor: Colors.black,
      expandedHeight: kToolbarHeight,
      //最少
      flexibleSpace: Container(),
    ),
  );
}

//调用此方法，即可得到固定在顶部的SliverAppBar，推不走的
SliverPersistentHeader setFFSliverAppBar({
  required Widget child,
  required double height,
  FFSliverParams? params,
}) {
  return SliverPersistentHeader(
    pinned: true,
    delegate: _SliverAppBarDelegate(
      child: child,
      height: height,
      params: params,
    ),
  );
}

//配合NestedScrollView一起使用，成为headerSliverBuilder的参数
Widget setFFNestedSliverAppbar({
  required double expandedHeight,
  double? collapsedHeight,
  required Widget child,
  // true:推上去 只会颜色背景颜色  false:推上去 只会显示图片的一部分
  bool isFlexible = true,
  Color? backgroundColor,
  Widget? leading,
}) {
  return SliverAppBar(
    leading: leading ?? Container(),
    floating: false,
    pinned: true,
    elevation: 0.0,
    backgroundColor: backgroundColor,
    expandedHeight: expandedHeight,
    collapsedHeight: collapsedHeight,
    flexibleSpace: isFlexible
        ? FlexibleSpaceBar(
      background: child,
    )
        : child,
  );
}

//调用此方法，即可得到悬浮在顶部的SliverAppBar,可以被推走的
//尽量保证高度大于0
Widget setFFSliverFloatAppBar({
  required double height,
  required BuildContext context,
  required Widget child,
  FFSliverParams? params,
  bool floating = false,
  Color? backgroundColor,
  double? toolbarHeight,
}) {
  return MediaQuery.removePadding(
    context: context,
    removeBottom: true,
    removeTop: true,
    child: SliverAppBar(
      elevation: 0.0,
      leading: Container(),
      floating: floating,
      pinned: false,
      flexibleSpace: child,
      collapsedHeight: height,
      expandedHeight: height,
      backgroundColor: backgroundColor,
      toolbarHeight: toolbarHeight ?? 0,
    ),
  );
}

class _FFAppBar extends StatelessWidget implements PreferredSizeWidget {
  Widget child;
  double height;

  _FFAppBar({
    Key? key,
    required this.child,
    required this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return child;
  }

  @override
  Size get preferredSize => Size.fromHeight(height);
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  Widget child;
  double height;
  FFSliverParams? params;

  _SliverAppBarDelegate({
    required this.child,
    required this.height,
    this.params,
  });

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return _FFAppBar(
      height: height,
      child: child,
    );
  }

  @override
  double get maxExtent => params?.maxExtent ?? height;

  @override
  double get minExtent => params?.minExtent ?? height;

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return false;
  }
}

class FFSliverParams {
  double? maxExtent;
  double? minExtent;

  FFSliverParams({
    this.maxExtent = 0,
    this.minExtent = 0,
  });
}
